package com.wf.smartcomm.server.client.action;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Bill;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.Payment;
import com.wf.smartcomm.server.response.ResponseCode;
import com.wf.smartcomm.server.service.BillService;
import com.wf.smartcomm.server.service.HouseService;
import com.wf.smartcomm.server.service.PaymentService;
import com.wf.smartcomm.server.util.ConstantUtils;

@Controller
@Scope("prototype")
public class PayAction extends BaseAction implements ModelDriven<Payment> {
	
	private Payment payment = new Payment();
	
	@Resource
	private HouseService houseService;
	
	@Resource
	private BillService billService;
	
	@Resource
	private PaymentService paymentService;
	
	public void pay() throws Exception {
		String houseId = request.getParameter("houseId");
		String billId = request.getParameter("billId");
		House house = houseService.getById(houseId);
		Bill bill = billService.get(billId);
		payment.setBillId(billId);
		payment.setHouse(house);
		payment.setUser(house.getUser());
		bill.setStatus(ConstantUtils.BILL_STATUS_PAYED);
		house.setNotifyDate(payment.getToAt());
		paymentService.save(payment);
		billService.update(bill);
		houseService.update(house);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		printJsonData(gson.toJson(map));
	}

	@Override
	public Payment getModel() {
		return payment;
	}

}
